package com.elinkway.infinitemovies.bigdata;

import android.text.TextUtils;

import com.lvideo.http.bean.LVideoBaseBean;
import com.lvideo.http.bean.LVideoDataHull;

/**
 *  日常上报
 */
public class BigDataLaunchQuitActionTask extends BigDataBaseTask {
    private String ap_type;
    private long ap_timespan;
    private long ap_timeStart;

    /**
     * 设置acode
     *
     * @param acode {@link BigDataConstants#ACODE_LAUNCH},
     *              {@link BigDataConstants#ACODE_PUSH},
     *              {@link BigDataConstants#ACODE_QUIT},
     * @return
     */
    public BigDataLaunchQuitActionTask setAcode(String acode) {
        putParam("acode", acode);
        return this;
    }

    /**
     * 设置apType
     *
     * @param apType {@link BigDataConstants#AP_LAUNCH_BACK},
     *               {@link BigDataConstants#AP_LAUNCH_PUSH},
     *               {@link BigDataConstants#AP_LAUNCH_USER},
     *               {@link BigDataConstants#AP_QUIT_BACKOFF},
     *               {@link BigDataConstants#AP_QUIT_OFF},
     * @return
     */
    public BigDataLaunchQuitActionTask setApType(String apType) {
        ap_type = apType;
        return this;
    }

    /**
     * 事件结果
     *
     * @param ar {@link BigDataConstants#AR_FAILED},
     *           {@link BigDataConstants#AR_SUCCESS}
     * @return
     */
    public BigDataLaunchQuitActionTask setAR(String ar) {
        putParam("ar", ar);
        return this;
    }

    /**
     * 设置网络类型
     *
     * @param netType
     * @return
     */
    public BigDataLaunchQuitActionTask setNT(String netType) {
        putParam("nt", netType);
        return this;
    }

    /**
     * 计时器开始，用于记录时间时间间隔
     */
    public void onEventStart() {
        ap_timeStart = System.currentTimeMillis();
    }

    /**
     * 计时器结束，用于记录时间时间间隔，必须在setApType调用过之后调用
     */
    public void onEventStop(boolean isQuitAction) {
        ap_timespan = System.currentTimeMillis() - ap_timeStart;
        StringBuilder ap = new StringBuilder();
        ap.append(isQuitAction ? BigDataConstants.AP_KEY_OFF_TYPE : BigDataConstants.AP_KEY_START_TYPE)
                .append("=")
                .append(ap_type)
                .append("&")
                .append(BigDataConstants.AP_KEY_TIME)
                .append("=")
                .append(ap_timespan);

        putParam("ap", ap.toString());
    }

    /**
     * 检测参数
     *
     * @return
     */
    public boolean checkArgs() {
        String message = "";
        LogUtils.d("post action is ready to go, params:" + params.toString());
        if (TextUtils.isEmpty(getParam("ar"))) {
            message = "ar is null,  call setAR(ar) first.";
        } else if (TextUtils.isEmpty(getParam("ap"))) {
            message = "ap is null, you should call following methods : setApType(ar) --> onEventStart() --> onEventStop()";
        } else if (TextUtils.isEmpty(getParam("acode"))) {
            message = "acode is null, call setAcode(acode) first. ";
        } else if (TextUtils.isEmpty(getParam("nt"))) {
            message = "nt is null, call setNT(nt) first. ";
        }
        if (!TextUtils.isEmpty(message)) {
            LogUtils.w(message);
            return false;
        }
        return true;
    }

    @Override
    public boolean execute() {
        if (checkArgs()) {
            LVideoDataHull<LVideoBaseBean> d = BigDataApi.postLaunchQuitAction(null, getParam("ap"), getParam("ar"), getParam("acode"), getParam("nt"));
            return d.getDataType() == LVideoDataHull.DataType.DATA_PARSER_IS_NULL;
        }
        return false;
    }

}
